/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.example;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * Skeleton for a Flink DataStream Job.
 *
 * <p>For a tutorial how to write a Flink application, check the
 * tutorials and examples on the <a href="https://flink.apache.org">Flink Website</a>.
 *
 * <p>To package your application into a JAR file for execution, run
 * 'mvn clean package' on the command line.
 *
 * <p>If you change the name of the main class (with the public static void main(String[] args))
 * method, change the respective entry in the POM.xml file (simply search for 'mainClass').
 */

public class DataStreamJob {
	public static class Person {
		public String name;
		public Integer age;
		public Person() {}
		public Person(String name, Integer age) {
			this.name = name;
			this.age = age;
		}
		public String toString() {
			return this.name + ": age " + this.age.toString();
		}
	}

	public static void main(String[] args) throws Exception {
		// Sets up the execution environment, which is the main entry point
		// to building Flink applications.
		final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//		DataStream<Person> flinkstones = env.fromElements(
//				new Person("tom", 22),
//				new Person("cat", 33),
//				new Person("jerry", 2)
//		);
		System.out.println(getFormatedTime() + " start!");
		List<Person> l = new ArrayList<Person>();
		l.add(new Person("tom", 22));
		l.add(new Person("cat", 33));
		l.add(new Person("jerry", 2));
		DataStream<Person> flinkstones = env.fromCollection(l);
		DataStream<Person> adults = flinkstones.filter(new FilterFunction<Person>() {
			@Override
			public boolean filter(Person person) throws Exception {
				return person.age >= 18;
			}
		});
		adults.print();
		// Execute program, beginning computation.
		env.execute("Flink Java API Skeleton");
		System.out.println(getFormatedTime() + " finished!");

	}

	public static String getFormatedTime() {
		// 创建一个LocalTime对象，表示当前时间（或者你可以使用特定的时间）
		LocalTime time = LocalTime.now(); // 或者使用LocalTime.of(hour, minute, second, nanoOfSecond)

		// 定义格式器，包括小时、分钟、秒和毫秒
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss,SSS");

		// 使用格式器格式化时间
		return time.format(formatter);// 输出类似于 "17:09:30,828" 的格式
	}
}
